<template>
  <div id="myList" class="myList">
    <ul>
      <li v-for="(item,index) in list" :key="index">
          <!--删除-->
          <el-popover placement="top" v-if="is_delete">
              <p>确定删除吗？</p>
              <div style="text-align: right; margin: 10px 0 0">
                  <el-button type="primary" size="mini" @click="deleteCollect(item.id)">确定</el-button>
              </div>
              <i class="el-icon-close delete" slot="reference"></i>
          </el-popover>
          <!--商品跳转详情页面-->
          <router-link :to="'/detail/' + item.id">
              <img :src="item.head_img_url" :alt="item.name" :title="item.name" />
              <h2>{{item.name}}</h2>
              <h3>{{item.description}}</h3>
              <p>
                <span>{{item.price}}元</span>
              </p>
          </router-link>
      </li>
        <router-link tag="li" id="more" v-if="more" :to="{path: '/products', query: {cate_id: cateID}}">
            浏览更多
            <i class="el-icon-d-arrow-right"></i>
        </router-link>
    </ul>
  </div>
</template>
<script>
export default {
  name: "MyList",
  data() {
    return {

    }
  },
  props: {
    list: {
      type: Array
    },
    more: {
        type: Boolean
    },
    is_delete: {
        type: Boolean,
        default: false
    }
  },
  computed: {
      // 获取栏目id
    cateID() {

        let cate_id = null;
        for (let i = 0; i < this.list.length; i++) {
            cate_id = this.list[i]['cate_id'];
        }
        return cate_id;
    }
  },
  methods: {
      // 删除商品
      deleteCollect(product_id) {
          let json_userInfo = localStorage.getItem('userInfo');
          let userInfo = JSON.parse(json_userInfo);
          this.$axios({
              method: 'get',
              url: this.baseUrl + '/api/v1/user/collectDel/' + product_id,
              headers: {
                  'access-token': userInfo.token,
                  'telephone': userInfo.telephone
              }
          }).then(response => {
              if (response.data.message = "删除收藏商品成功" && response.data.status == 200) {
                  const h = this.$createElement;
                  this.$notify({
                      title: '删除收藏成功',
                      message: h('i', {style: 'color: teal'}, '删除收藏商品成功')
                  })
                  this.$router.go(0)  //刷新当前页面

              }
          }).catch(error => {
              console.log(error.response)
          })
      },
  }

};
</script>
<style scoped>
.myList ul li {
  z-index: 1;
  float: left;
  width: 234px;
  height: 280px;
  padding: 10px 0;
  margin: 0 0 14.5px 13.7px;
  background-color: white;
  -webkit-transition: all 0.2s linear;
  transition: all 0.2s linear;
  position: relative;
}
.myList ul li:hover {
  z-index: 2;
  -webkit-box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  -webkit-transform: translate3d(0, -2px, 0);
  transform: translate3d(0, -2px, 0);
}
.myList ul li img {
  display: block;
  width: 160px;
  height: 160px;
  background: url(../assets/imgs/placeholder.png) no-repeat 50%;
  margin: 0 auto;
}
.myList ul li h2 {
  margin: 25px 10px 0;
  font-size: 14px;
  font-weight: 400;
  color: #333;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
.myList ul li h3 {
  margin: 5px 10px;
  height: 18px;
  font-size: 12px;
  font-weight: 400;
  color: #b0b0b0;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
.myList ul li p {
  margin: 10px 10px 10px;
  text-align: center;
  color: #ff6700;
}
.myList ul li p .del {
  margin-left: 0.5em;
  color: #b0b0b0;
  text-decoration: line-through;
}
.myList #more {
  text-align: center;
  line-height: 280px;
}
.myList #more a {
  font-size: 18px;
  color: #333;
}
.myList #more a:hover {
  color: #ff6700;
}
.myList ul li .delete {
  position: absolute;
  top: 10px;
  right: 10px;
  display: none;
}
.myList ul li:hover .delete {
  display: block
}
.myList ul li .delete:hover {
  color: #ff6700;
}
</style>
